import { Injectable, HttpException } from '@nestjs/common';
// import { CreateUserDto } from './dto/create-user.dto';
// import { UpdateUserDto } from './dto/update-user.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { UserEntity } from './entities/user.entity';
import { QueryUserPageDto } from './dto/query-user-page.dto';
import * as bcryptjs from 'bcryptjs';

@Injectable()
export class UserService {
  constructor(
    @InjectRepository(UserEntity)
    private readonly userRepository: Repository<UserEntity>,
  ) {}
  // 创建用户
  async create(user: Partial<UserEntity>): Promise<UserEntity> {
    const { username, email, password } = user;
    if (!username || !email) {
      throw new HttpException('用户名和邮箱不能为空', 400);
    }
    if (!password) {
      throw new HttpException('密码不能为空', 400);
    }
    const userExist = await this.userRepository.findOne({
      where: { username, del_flag: '0' },
    });
    if (userExist) {
      throw new HttpException('用户名已存在', 400);
    }
    user.created_time = new Date();
    user.updated_time = new Date();
    user.del_flag = '0';
    user.created_by = 'system';
    user.updated_by = 'system';
    user.password = await bcryptjs.hash(password, 10);
    let newUser = await this.userRepository.save(user);
    return newUser;
  }

  // 查询所有用户
  async findAll(): Promise<UserEntity[]> {
    // 返回所有用户,根据del_flag查询
    return await this.userRepository.find({ where: { del_flag: '0' } });
  }

  // 分页查询用户列表，不包含密码，可以根据用户名、邮箱、删除标志、创建时间、创建人进行模糊查询
  async findList(query: QueryUserPageDto): Promise<UserEntity[]> {
    const {
      page = 1,
      pageSize = 10,
      username,
      email,
      del_flag,
      created_by,
      created_time,
    } = query;
    const queryBuilder = this.userRepository.createQueryBuilder('user');
    if (username) {
      queryBuilder.andWhere('user.username LIKE :username', {
        username: `%${username}%`,
      });
    }
    if (email) {
      queryBuilder.andWhere('user.email LIKE :email', { email: `%${email}%` });
    }
    if (del_flag) {
      queryBuilder.andWhere('user.del_flag = :del_flag', { del_flag });
    }
    if (created_by) {
      queryBuilder.andWhere('user.created_by LIKE :created_by', {
        created_by: `%${created_by}%`,
      });
    }
    if (created_time) {
      queryBuilder.andWhere('user.created_time LIKE :created_time', {
        created_time: `%${created_time}%`,
      });
    }
    return await queryBuilder
      .skip((page - 1) * pageSize)
      .take(pageSize)
      .getMany();
  }

  // 查询单个用户
  async findOne(id: number): Promise<UserEntity> {
    let user = await this.userRepository.findOne({ where: { id } });
    if (!user) {
      throw new HttpException('用户不存在', 400);
    }
    return user;
  }

  // 更新用户信息
  async update(user: Partial<UserEntity>): Promise<UserEntity> {
    user.updated_time = new Date();
    user.updated_by = 'system';
    // 判断用户是否存在
    const userExist = await this.userRepository.findOne({
      where: { id: user.id, del_flag: '0' },
    });
    if (!userExist) {
      throw new HttpException('用户不存在', 400);
    }
    await this.userRepository.update(user.id, user);
    return await this.userRepository.findOne({ where: { id: user.id } });
  }

  // 删除用户
  async remove(id: number): Promise<UserEntity> {
    const user = await this.userRepository.findOne({ where: { id } });
    if (!user) {
      throw new HttpException('用户不存在', 400);
    }
    user.del_flag = '1';
    user.updated_time = new Date();
    user.updated_by = 'system';
    await this.userRepository.update(id, user);
    return user;
  }
}
